function toAudio(num) {
  var numStr;
  if (num == 0) {
    numStr = '零';
  }
  else {
    numStr = numToStr(num); 
  }
  
  numStr = numStr.replace('亿万', '亿');
  return encodeURI(numStr);
}

function numToStr(num) {
  var pattern = /零-/g;
  var numStr = num.toString();
  var dotPosition = numStr.indexOf('.');
  var numStrDit;
  if (dotPosition > -1) {
    firstNumStr = numToDit(numStr.slice(0, dotPosition), false).replace(pattern, '');
    secondNumStr = dotToDit(numStr.slice(dotPosition));
    numStrDit = firstNumStr + secondNumStr;
  }
  else {
    numStrDit = numToDit(numStr, false).replace(pattern, '');
  }
  return numStrDit;
}

function dotToDit(numStr) {
  var firstChar = numStr.slice(0, 1);
  var returnValue = '';
  var len = numStr.length;
  if (len == 0) {
    return '';
  }
  switch (firstChar) {
    case '1': returnValue = '壹'; break;
    case '2': returnValue = '贰'; break;
    case '3': returnValue = '叁'; break;
    case '4': returnValue = '肆'; break;
    case '5': returnValue = '伍'; break;
    case '6': returnValue = '陆'; break;
    case '7': returnValue = '柒'; break;
    case '8': returnValue = '捌'; break;
    case '9': returnValue = '玖'; break;
    case '0': returnValue = '零'; break;
    case '.': returnValue = '点'; break;
  }
  return returnValue + dotToDit(numStr.slice(1));
}

function numToDit(numStr, previousIsZero) {
  var numLen = numStr.length, isZero = false;
  var firstChar = numStr.slice(0, 1);
  var returnValue, numPow, isW = false;
  if (firstChar == '0') {
    if (previousIsZero) {
      returnValue = '';
    }
    isZero = true;
    switch (numLen) {
      case 1: returnValue = ''; isZero = false; isW = true; break;
      case 5: returnValue = '万'; isZero = false; isW = true; break;
      case 9: returnValue = '亿'; isZero = false; isW = true; break;
      case 13: returnValue = '万'; isZero = false; isW = true; break;
      case 17: returnValue = '亿亿'; isZero = false; isW = true; break;
      default: if (!previousIsZero) { returnValue = '零'; } break;
    }
  }
  else {
    switch (firstChar) {
      case '1': returnValue = '壹' + getPow(numLen); break;
      case '2': returnValue = '贰' + getPow(numLen); break;
      case '3': returnValue = '叁' + getPow(numLen); break;
      case '4': returnValue = '肆' + getPow(numLen); break;
      case '5': returnValue = '伍' + getPow(numLen); break;
      case '6': returnValue = '陆' + getPow(numLen); break;
      case '7': returnValue = '柒' + getPow(numLen); break;
      case '8': returnValue = '捌' + getPow(numLen); break;
      case '9': returnValue = '玖' + getPow(numLen); break;
    }
  }
  if (numLen == 0) {
    return '';
  }
  if (previousIsZero && isW) {
    return '-' + returnValue + numToDit(numStr.slice(1), isZero);
  }
  return returnValue + numToDit(numStr.slice(1), isZero);
}

function getPow(numLen) {
  var str;
  switch (numLen) {
    case 1: str = ''; break;
    case 2: str = '拾'; break;
    case 3: str = '佰'; break;
    case 4: str = '仟'; break;
    case 5: str = '万'; break;
    case 6: str = '拾'; break;
    case 7: str = '佰'; break;
    case 8: str = '仟'; break;
    case 9: str = '亿'; break;
    case 10: str = '拾'; break;
    case 11: str = '佰'; break;
    case 12: str = '仟'; break;
    case 13: str = '万'; break;
    case 14: str = '拾'; break;
    case 15: str = '佰'; break;
    case 16: str = '万'; break;
    case 17: str = '亿亿'; break;
  }
  return str;
}

function numAudio(num) {
  var title = 'http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=3&text=';
  var str = toAudio(num);
  var audio = document.createElement('audio');
  audio.autoplay = true;
  var source = document.createElement('source');
  source.type = 'audio/mpeg';
  source.src = title + str;
  audio.appendChild(source);
  document.body.appendChild(audio);
  audio.onended = function (ev) {
    audio.outerHTML = '';
  }
}